#!/usr/bin python
from plugins.config.config_package import *

keywords = [
    'CheckVul Success','CheckVul Failed','Server Connection Timeout'
]

class unauth(object):
    def __init__(self,host):
        self.host = host

    def redis(host):
        try:
            socket.setdefaulttimeout(1)
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((host, 6379))
            s.send(bytes("INFO\r\n", 'UTF-8'))
            result = s.recv(1024).decode()
            if "redis_version" in result:
                core.checksuc(target=host,name='Redis unauthorize',payload='INFO')
            else:
                pass
            s.close()
        except Exception as e:
            pass
        except KeyboardInterrupt:
            sys.exit()
        

    def memcached(host):
        try:
            socket.setdefaulttimeout(1)
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((host, 11211))
            s.send(bytes('stats\r\n', 'UTF-8'))
            if 'version' in s.recv(1024).decode():
                core.checksuc(target=host,name='Memcached unauthorize',payload='stats')
            else:
                pass
        except Exception as e:
            pass
        except KeyboardInterrupt:
            sys.exit()

    def elasticsearch(host):
        try:
            url = 'http://' + host + ':9200/_cat'
            r = requests.get(url, timeout=1)
            if '/_cat/master' in r.content.decode():
                core.checksuc(target=host,name='Elasticsearch unauthorize',payload='/_cat/master')
            else:
                pass
        except Exception as e:
            pass
        except KeyboardInterrupt:
            sys.exit()

    def zookeeper(host):
        try:
            socket.setdefaulttimeout(1)
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((host, 2181))
            s.send(bytes('envi', 'UTF-8'))
            data = s.recv(1024).decode()
            s.close()
            if 'Environment' in data:
                core.checksuc(target=host,name='Zookeeper unauthorize',payload='envi')
            else:
                pass
        except Exception as e:
            pass
        except KeyboardInterrupt:
            sys.exit()

    def docker(host):
        try:
            url = 'http://' + host + ':2375' + '/version'
            r = requests.get(url, timeout=1)
            if 'ApiVersion' in r.content.decode():
                core.checksuc(target=host,name='Docker unauthorize',payload='/version')
            else:
                pass
        except Exception as e:
            pass
        except KeyboardInterrupt:
            sys.exit()

    def hadoop(host):
        try:
            url = 'http://' + host + ':50070' + '/dfshealth.html'
            r = requests.get(url, timeout=1)
            if 'hadoop.css' in r.content.decode():
                core.checksuc(target=host,name='Hadoop unauthorize',payload='/dfshealth.html')
            else:
                pass
        except Exception as e:
            pass
        except KeyboardInterrupt:
            sys.exit()

    def jenkins(host):
        try:
            url = 'http://' + host + ':8080' + '/systemInfo'
            r = requests.get(url, timeout=1, verify=False)
            if 'jenkins.war' in r.content.decode() and 'JENKINS_HOME' in r.content.decode():
                core.checksuc(target=host,name='Jeckins unauthorize',payload='/systemInfo')
            else:
                pass
        except Exception as e:
            pass
        except KeyboardInterrupt:
            sys.exit()